<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;
use app\api\error\Money as MoneyError;
use app\api\error\Common as CommonError;
/**
 * 财务接口逻辑
 */
class Moneyapp extends ApiBase
{
	/**
     * 转账
     */
	public function moneyEdit($data){
		$getid=db('user')->where('username',$data['username'])->value('id');
		if(empty($getid)){
			return MoneyError::$zrMoneyError;
		}
		if($data['type'] == 1){
			$str='moneyMy';
			$type=1;
			$gtype=2;
		}elseif($data['type'] == 2){
			$str='count';
			$type=56;
			$gtype=57;
		}elseif($data['type'] == 3){
			$str='xfq';
			$type=104;
			$gtype=105;
		}
		$moneyMy=db('user')->where('id',$data['user_id'])->value($str);
		if($data['type'] == 2){
			if($moneyMy < $data['money']+250000){
				return MoneyError::$moneyError;
			}
		}else{
			if($moneyMy < $data['money']){
				return MoneyError::$moneyError;
			}
		}
		$username=db('user')->where('id',$data['user_id'])->value('phone');
		if( $msg = checkCode($username,$data['code'],3)){
			return $msg;
		}
		db('moneyyj')->startTrans();
		$res=moneyyj_log($data['money']*(-1),$data['user_id'],$type,$getid);
		$re=moneyyj_log($data['money'],$getid,$gtype,$data['user_id']);
		if($res && $re){
			db('moneyyj')->commit();
			return $this->apiReturn();	
		}else{
			db('moneyyj')->rollback();
			return MoneyError::$paramError;	
		}
		
	}
	/**
     * 配置信息
     */
    public function txConfig($data){
		if(!empty($data['user_id'])){
			$txinfo=db('moneyapp')->field('bank,bankName,bankUser')->where('user_id',$data['user_id'])->order('id desc')->find();
		}else{
			$txinfo=array();
		}
		$arr=array(
			'config'=>parse_config_array('tx_config'),
			'banklist'=>db('bank')->select(),
			'txinfo'=>$txinfo
		);
		return $arr;		
	}
	 /**
     * 提现申请列表
     */
    public function getMoneyappList($data){
		$where=[];
		!empty($data['user_id']) && $where['user_id'] = $data['user_id'];
		return $this->modelMoneyapp->getList($where);
	}
	 /**
     * 撤销提现
     */
    public function delMoneyapp($data){
		db('moneyapp')->startTrans();
		$info=db('moneyapp')->where('id',$data['id'])->find();
		if(empty($info) || $info['stat']!=0 ){
			return MoneyError::$paramError;
		}
		$res=db('moneyapp')->where(array('id'=>$data['id'],'user_id'=>$data['user_id'],'stat'=>0))->delete();
		$re=moneyyj_log($info['money'],$info['user_id'],5,$info['user_id']);
		if ($res && $re) {
			db('moneyapp')->commit();
			return $this->apiReturn();	
		}else{
			db('moneyapp')->rollback();
			return MoneyError::$paramError;
		}
	}
	/**
     * 提现申请
     */
	public function applyTx($data){
		$username=db('user')->where('id',$data['user_id'])->value('phone');
		if( $msg = checkCode($username,$data['code'],4)){
			return $msg;
		}
		$moneyMy=db('user')->where('id',$data['user_id'])->value('moneyMy');
		if($moneyMy < $data['money']){
			return MoneyError::$moneyError;
		}
		$arr=parse_config_array('tx_config');
//		if($data['money']%$arr['bs'] != 0){
//			return MoneyError::$paramError;
//		}
		if(!empty($data['type'])){
			$s='d1';
		}else{
			$s='d0';
		}
		$data['moneyTax']=$data['money']*$arr[$s];
        $data['moneyMy']=$data['money']*(1-$arr[$s]);
		$data['create_time']=time();
		db('moneyapp')->startTrans();
		$res=db('moneyapp')->insert($data);
		$re=moneyyj_log($data['money']*(-1),$data['user_id'],4,$data['user_id']);
		if($res && $re){
			db('moneyapp')->commit();
			return $this->apiReturn();	
		}else{
			db('moneyapp')->rollback();
			return MoneyError::$paramError;	
		}
		
	}
	
}
